//
//  SharingViewController.m
//  phmall
//
//  Created by admin on 2017/11/3.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "ShareViewController.h"
#import "ShareListCell.h"
#import "ShareDetailsViewController.h"
#import "CommodityDetailsVC.h"
#import "FUIImageView.h"
#import "ShareHotNewView.h"
@interface ShareViewController ()<UIScrollViewDelegate,ShareHotNewViewDelegate>{
    FUIImageView    *_headView;
    UIView          *_flage;
    UIButton        *supply;
    UIButton        *buy;
}
@property(strong,nonatomic)UIScrollView   *scrollView;
@end

@implementation ShareViewController
-(void)network:(BOOL)type{
    [buy setSelected:type];
    [supply setSelected:type];
    _scrollView.scrollEnabled=type;
}
-(void)purchase:(NSString *)uid{
    self.hidesBottomBarWhenPushed=YES;
    CommodityDetailsVC *sear=[[CommodityDetailsVC alloc] init];
    sear.uid=uid;
    [self.navigationController pushViewController:sear animated:YES];
    self.hidesBottomBarWhenPushed=NO;
}
-(void)particulars:(NSString *)uid{
    self.hidesBottomBarWhenPushed=YES;
    ShareDetailsViewController *sear=[[ShareDetailsViewController alloc] init];
    sear.uid=uid;
    [self.navigationController pushViewController:sear animated:YES];
    self.hidesBottomBarWhenPushed=NO;
}
-(void)addFriends:(NSString *)uid{
    NSString *urlstr =  [NSString stringWithFormat:@"%@%@",HomeURL,@"share/friend"];
    NSMutableDictionary *parametersDic = [NSMutableDictionary dictionary];
    [parametersDic setObject:uid forKey:@"uid"];
    [httpTool ZBPostNetDataWith:urlstr withDic:parametersDic andSuccess:^(id responseObject) {
        NSMutableDictionary *responseDict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:nil];
        if ([responseDict[@"ret"] isEqualToString:@"200"]) {
            if (responseDict[@"data"] !=[NSNull alloc]) {
                NSString *data=[NSString stringWithFormat:@"%@",responseDict[@"data"]];
                if ([data isEqualToString:@"1"]) {
                    NSLog(@"添加成功");
                    [PublicClass ProgressHUDModeText:Localized(@"添加成功") andTime:2];
                }else{
                    NSLog(@"添加失败");
                    [PublicClass ProgressHUDModeText:Localized(@"添加失败") andTime:2];
                }
            }
        }else{
            [self userLogout];
//           [PublicClass ProgressHUDModeText:@"账号未登录或登录已过期" andTime:2];
        }
    } andFailure:^{
        [PublicClass ProgressHUDModeText:Localized(@"网络错误") andTime:2 ];
        //        [SVProgressHUD showErrorWithStatus:@"网络错误"];
    }];
}
-(void)leftBarAction{
    
}
-(void)rightBarAction{
  
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navBarBgAlpha=@"1.0";
    // 改变NavBar顶层View的颜色
    self.navigationController.navigationBar.barTintColor =  [UIColor orangeColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];

    UIBarButtonItem* rightBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"消息"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(rightBarAction)];
    
    self.navigationItem.rightBarButtonItem=rightBar;
    
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"搜索白色"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    self.navigationItem.leftBarButtonItem=leftBar;
    
    // 设置子类的NavBar上的渲染颜色
    self.navigationController.navigationBar.tintColor = [UIColor clearColor];
    // 改变NavBar底层View的颜色
    self.navigationController.navigationBar.backgroundColor = [UIColor clearColor];
    // 改变NavBar顶层View的颜色
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
    // Do any additional setup after loading the view.

    if ([self respondsToSelector:@selector(setEdgesForExtendedLayout:)]) {
        self.edgesForExtendedLayout = UIRectEdgeNone;
    }
    [self getNavBarView];
    
    
    _scrollView = [[UIScrollView alloc]init];
    _scrollView.bounces = NO;
    _scrollView.pagingEnabled = YES;
    _scrollView.delegate = self;
    [self.view addSubview:_scrollView];
    _scrollView.backgroundColor=[UIColor redColor];
    _scrollView.showsVerticalScrollIndicator = FALSE;
    _scrollView .showsHorizontalScrollIndicator = FALSE;
    _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH*2, SCREEN_HEIGHT-(STATUS_BAR_HEIGHT+44)-self.tabBar_height-49);
    //
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(0);
        make.left.right.equalTo(self.view).priorityHigh();
        make.bottom.equalTo(self.view).with.offset(0).priorityHigh();
    }];
    [_scrollView.superview layoutIfNeeded];
    
    
    ShareHotNewView *hot=[[ShareHotNewView alloc]init];
    hot.type=@"hot";
    hot.delegate=self;
    hot.backgroundColor=[UIColor blueColor];
    [_scrollView addSubview:hot];
    [hot mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView).offset(0);
        make.left.equalTo(_scrollView).priorityHigh();
        make.height.offset(SCREEN_HEIGHT-(STATUS_BAR_HEIGHT+44)-self.tabBar_height-49);
        make.width.offset(SCREEN_WIDTH);
    }];
    
    ShareHotNewView *new=[[ShareHotNewView alloc]init];
    new.type=@"new";
    new.delegate=self;
    [_scrollView addSubview:new];
    [new mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView).offset(0);
        make.left.equalTo(hot.mas_right).priorityHigh();
        make.height.offset(SCREEN_HEIGHT-(STATUS_BAR_HEIGHT+44)-self.tabBar_height-49);
        make.width.offset(SCREEN_WIDTH);
    }];
}
#pragma mark-选中scorllview来调整headvie的选中
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
   
    NSInteger index = scrollView.contentOffset.x/scrollView.bounds.size.width;
    NSLog(@"%f------%ld",scrollView.contentOffset.x,(long)index);
    if (index==0) {
        [UIView animateWithDuration:0.2 animations:^{
            [_flage mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(40);
                make.centerX.equalTo(supply);
                make.bottom.equalTo(supply);
                make.height.offset(3);
            }];
            [_flage.superview layoutIfNeeded];
        }];
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            [_flage mas_remakeConstraints:^(MASConstraintMaker *make) {
                make.width.offset(40);
                make.centerX.equalTo(buy);
                make.bottom.equalTo(buy);
                make.height.offset(3);
            }];
            [_flage.superview layoutIfNeeded];
        }];
    }
}
-(void)getNavBarView{
    _headView = [[FUIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH-120, 44)];
    _headView.contentMode= UIViewContentModeScaleAspectFit;
    _headView.userInteractionEnabled=YES;
    _headView.backgroundColor=[UIColor clearColor];
    self.navigationItem.titleView = _headView; //设置导航栏的titleView为imageView
    
    supply=[UIButton buttonWithType:UIButtonTypeCustom];
    [supply setTitle:Localized(@"最新") forState:UIControlStateNormal];
    supply.titleLabel.font=[UIFont systemFontOfSize:18];
    [supply setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [supply addTarget:self action:@selector(suppyAction:) forControlEvents:UIControlEventTouchUpInside];
    [_headView addSubview:supply];
    [supply mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(70).priorityHigh();
        make.centerY.offset(0).priorityHigh();
        make.centerX.offset(-40).priorityHigh();
    }];
    
    buy=[UIButton buttonWithType:UIButtonTypeCustom];
    [buy setTitle:Localized(@"最热") forState:UIControlStateNormal];
    buy.titleLabel.font=[UIFont systemFontOfSize:18];
    [buy addTarget:self action:@selector(buyAction:) forControlEvents:UIControlEventTouchUpInside];
    [buy setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_headView   addSubview:buy];
    [buy mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(70).priorityHigh();
        make.centerY.offset(0).priorityHigh();
        make.centerX.offset(40).priorityHigh();
    }];
    
    _flage=[[UIView alloc]init];
    _flage.backgroundColor=[UIColor whiteColor];
    [_headView addSubview:_flage];
    [_flage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(40);
        make.centerX.equalTo(supply);
        make.bottom.equalTo(supply);
        make.height.offset(3);
    }];
}
-(void)buyAction:(UIButton *)sender{
    [UIView animateWithDuration:0.2 animations:^{
        _scrollView.contentOffset = CGPointMake(SCREEN_WIDTH, 0);
        [_flage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(40);
            make.centerX.equalTo(sender);
            make.bottom.equalTo(sender);
            make.height.offset(3);
        }];
        [_flage.superview layoutIfNeeded];
    }];
}
-(void)suppyAction:(UIButton *)sender{
    [UIView animateWithDuration:0.2 animations:^{
        _scrollView.contentOffset = CGPointMake(0, 0);
        [_flage mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(40);
            make.centerX.equalTo(sender);
            make.bottom.equalTo(sender);
            make.height.offset(3);
        }];
        [_flage.superview layoutIfNeeded];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
